package com.start_eternity.oasissafewalk.controller;
import com.start_eternity.oasissafewalk.comment.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {
    @PostMapping("/upload")
    public Result upload(MultipartFile driverLicense) throws Exception {
        log.info("文件上传:{}",driverLicense);
        //获取原始文件名 -1.jpg
        String originalFilename = driverLicense.getOriginalFilename();

        //构造唯一文件名--uuid(通用唯一识别码)51a4c9bc-d128-4271-86cc-9026aba94a12
        int index = originalFilename.lastIndexOf(".");
        String extname = originalFilename.substring(index);
        String newFileName= UUID.randomUUID().toString()+extname;
        log.info("新的文件名:{}",newFileName);

        //将文件存储在服务器的磁盘目录中E:\images
        driverLicense.transferTo(new File("E:\\images\\"+newFileName));
        return Result.success();
    }
}
